// pages/student/research/research.js
const app = getApp();

Page({
  data: {
    researchList: [],
    stats: {
      total: 0,
      verified: 0,
      pending: 0,
      score: 0,
    },
  },

  onLoad() {
    this.loadResearchList();
  },

  onShow() {
    this.loadResearchList();
  },

  async loadResearchList() {
    try {
      const local = require("../../../utils/local");

      // 检查用户登录状态
      const currentUser = local.getCurrentUser();
      console.log("当前用户:", currentUser);

      if (!currentUser) {
        console.log("用户未登录，无法加载科研成果");
        wx.showToast({
          title: "请先登录",
          icon: "none",
          duration: 2000,
        });
        // 跳转到登录页面
        setTimeout(() => {
          wx.navigateTo({
            url: "/pages/login/login",
          });
        }, 2000);
        return;
      }

      const result = local.getResearchList();
      console.log("科研成果列表结果:", result);

      if (result.success) {
        console.log("科研成果原始数据:", result.data.list);
        this.setData({
          researchList: result.data.list,
          stats: result.data.stats,
        });
        console.log("成功加载科研成果:", result.data.list.length, "条");
        // 打印每个科研成果的ID
        result.data.list.forEach((item, index) => {
          console.log(`科研成果${index + 1}:`, {
            id: item.id,
            title: item.title,
            type: item.type,
          });
        });
      } else {
        console.error("获取科研成果失败:", result.message);
        wx.showToast({
          title: result.message || "加载失败",
          icon: "error",
        });
      }
    } catch (error) {
      console.error("加载科研成果失败:", error);
      wx.showToast({
        title: "加载失败，请重试",
        icon: "error",
      });
    }
  },

  // 远程获取方法移除，改为本地

  addResearch() {
    wx.navigateTo({
      url: "/pages/student/research/add/add",
    });
  },

  viewResearch(e) {
    const id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/student/research/detail/detail?id=${id}`,
    });
  },

  editResearch(e) {
    console.log("编辑科研成果事件:", e);
    const id = e.currentTarget.dataset.id;
    console.log("编辑的科研成果ID:", id);

    // 阻止事件冒泡
    if (e.stopPropagation) {
      e.stopPropagation();
    }

    if (!id) {
      wx.showToast({
        title: "无法获取科研成果ID",
        icon: "error",
      });
      return;
    }

    wx.navigateTo({
      url: `/pages/student/research/edit/edit?id=${id}`,
    });
  },

  deleteResearch(e) {
    console.log("删除科研成果事件:", e);
    const id = e.currentTarget.dataset.id;
    console.log("删除的科研成果ID:", id);

    // 阻止事件冒泡
    if (e.stopPropagation) {
      e.stopPropagation();
    }

    if (!id) {
      wx.showToast({
        title: "无法获取科研成果ID",
        icon: "error",
      });
      return;
    }

    wx.showModal({
      title: "确认删除",
      content: "删除后无法恢复，确认删除这个科研成果吗？",
      success: (res) => {
        if (res.confirm) {
          this.doDeleteResearch(id);
        }
      },
    });
  },

  async doDeleteResearch(id) {
    wx.showLoading({ title: "删除中..." });

    try {
      const local = require("../../../utils/local");
      const result = local.deleteResearch(id);
      if (result.success) {
        wx.showToast({
          title: "删除成功",
          icon: "success",
        });
        this.loadResearchList();
      } else {
        wx.showToast({
          title: result.message || "删除失败",
          icon: "error",
        });
      }
    } catch (error) {
      console.error("删除科研成果失败:", error);
      wx.showToast({
        title: "网络错误，请重试",
        icon: "error",
      });
    } finally {
      wx.hideLoading();
    }
  },

  // 远程删除方法删除

  previewFile(e) {
    const url = e.currentTarget.dataset.url;

    // 阻止事件冒泡
    if (e.stopPropagation) {
      e.stopPropagation();
    }

    wx.previewImage({
      urls: [url],
      current: url,
    });
  },

  // 阻止事件冒泡
  stopPropagation(e) {
    console.log("stopPropagation被调用:", e);
    // 在微信小程序中，事件对象可能没有stopPropagation方法
    // 我们直接返回，不执行任何操作
    return;
  },
});
